package com.cskaoyan.wxcontroller;

import com.cskaoyan.bean.aftersale.AfterSaleDetailVo;
import com.cskaoyan.bean.aftersale.AfterSaleWxVo;
import com.cskaoyan.bean.baseresultVO.BaseParam;
import com.cskaoyan.bean.baseresultVO.BaseRespVo;
import com.cskaoyan.bean.domain.AfterSale;
import com.cskaoyan.bean.domain.Order;
import com.cskaoyan.bean.domain.User;
import com.cskaoyan.bean.order.OrderId.OrderId;
import com.cskaoyan.service.AfterSaleService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.expression.spel.ast.NullLiteral;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.UUID;

/**
 * @author looper
 * @
 **/
@RequestMapping("wx/aftersale")
@RestController
public class WxAftersaleController {

    @Autowired
    AfterSaleService service;

    @PostMapping("submit")
    public BaseRespVo getSubmit(@RequestBody AfterSale afterSale, String reason, String typeDesc) {

        //把这个订单的售后状态改为1
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipals().getPrimaryPrincipal();
        //用户id 插入一条新的售后订单
        afterSale.setUserId(user.getId());
        afterSale.setComment(reason);
        afterSale.setAftersaleSn(UUID.randomUUID().toString().substring(0, 6));
        afterSale.setStatus(Short.valueOf("1"));
        afterSale.setAddTime(LocalDateTime.now());
        //根据订单号更新订单
        Order order = new Order();
        // TODO: 2021/12/5
        order.setOrderStatus((short) 202);
        order.setId(afterSale.getOrderId());
        order.setRefundAmount(afterSale.getAmount());
        order.setRefundType(typeDesc);
        order.setRefundTime(LocalDateTime.now());
        order.setRefundContent(reason);
        Integer code = service.getSubmit(afterSale, order);
        return BaseRespVo.ok(null);

    }

    @GetMapping("list")
    public BaseRespVo getList(AfterSale afterSale, BaseParam baseParam) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipals().getPrimaryPrincipal();

        //获得status
        afterSale.setUserId(user.getId());
        //获得user 根据userid找到他的所有售后单号

        //找到订单号,从ordergood表里找到所有属于这个订单号的goods

        AfterSaleWxVo afterSaleWxVo = service.getListForWx(afterSale, user, baseParam);

        return BaseRespVo.ok(afterSaleWxVo);
    }

    @GetMapping("detail")
    public BaseRespVo getDetailAftersaleForWx(OrderId orderId) {

        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipals().getPrimaryPrincipal();


        Order order = new Order();
        order.setUserId(user.getId());
        //该订单号
        order.setId(orderId.getOrderId());

        //显示这个订单号的订单信息+
        // 售后表下这个用户的这个订单的售后信息
        //订单goods表下对应的所有商品集合
        AfterSaleDetailVo afterSaleDetailVo = service.getDetailAftersaleForWx(order);
        return BaseRespVo.ok(afterSaleDetailVo);

    }


}
